<template>
	<el-main class="bg edit_wrap">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
			<el-col v-if="user_group === '管理员' || $check_field('get','student_name') || $check_field('add','student_name') || $check_field('set','student_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生姓名" prop="student_name">
					<el-input id="student_name" v-model="form['student_name']" placeholder="请输入学生姓名"
							  v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','student_name')) || (!form['personal_information_id'] && $check_field('add','student_name'))" :disabled="disabledObj['student_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','student_name')">{{form['student_name']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','student_number') || $check_field('add','student_number') || $check_field('set','student_number')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生学号" prop="student_number">
						<div v-if="user_group !== '管理员'">
							{{ get_user_session_student_number(form['student_number']) }}
							<!--<el-input id="business_name" v-model="form['student_number']" placeholder="请输入学生学号"-->
							<!--v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','student_number')) || (!form['personal_information_id'] && $check_field('add','student_number'))" :disabled="disabledObj['student_number_isDisabled']"></el-input>-->
							<!--<div v-else-if="$check_field('get','student_number')">{{form['student_number']}}</div>-->
							<el-select v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','student_number')) || (!form['personal_information_id'] && $check_field('add','student_number'))" id="student_number" v-model="form['student_number']" :disabled="disabledObj['student_number_isDisabled']">
								<el-option v-for="o in list_user_student_number" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','student_number')" id="student_number" v-model="form['student_number']" :disabled="true">
								<el-option v-for="o in list_user_student_number" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="student_number" v-model="form['student_number']" :disabled="disabledObj['student_number_isDisabled']">
							<el-option v-for="o in list_user_student_number" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','student_gender') || $check_field('add','student_gender') || $check_field('set','student_gender')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生性别" prop="student_gender">
					<el-select id="student_gender" v-model="form['student_gender']"
						v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','student_gender')) || (!form['personal_information_id'] && $check_field('add','student_gender'))">
						<el-option v-for="o in list_student_gender" :key="o" :label="o" :value="o">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','student_gender')">{{form['student_gender']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','student_age') || $check_field('add','student_age') || $check_field('set','student_age')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生年龄" prop="student_age">
					<el-input id="student_age" v-model="form['student_age']" placeholder="请输入学生年龄"
							  v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','student_age')) || (!form['personal_information_id'] && $check_field('add','student_age'))" :disabled="disabledObj['student_age_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','student_age')">{{form['student_age']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','class_name') || $check_field('add','class_name') || $check_field('set','class_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="班级名称" prop="class_name">
					<el-input id="class_name" v-model="form['class_name']" placeholder="请输入班级名称"
							  v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','class_name')) || (!form['personal_information_id'] && $check_field('add','class_name'))" :disabled="disabledObj['class_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','class_name')">{{form['class_name']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','dormitory_building_id') || $check_field('add','dormitory_building_id') || $check_field('set','dormitory_building_id')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宿舍楼" prop="dormitory_building_id">
					<el-input id="dormitory_building_id" v-model="form['dormitory_building_id']" placeholder="请输入宿舍楼ID"
							  v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','dormitory_building_id')) || (!form['personal_information_id'] && $check_field('add','dormitory_building_id'))" :disabled="disabledObj['dormitory_building_id_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','dormitory_building_id')">{{form['dormitory_building_id']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','dormitory_id') || $check_field('add','dormitory_id') || $check_field('set','dormitory_id')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="宿舍" prop="dormitory_id">
					<el-input id="dormitory_id" v-model="form['dormitory_id']" placeholder="请输入宿舍ID"
							  v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','dormitory_id')) || (!form['personal_information_id'] && $check_field('add','dormitory_id'))" :disabled="disabledObj['dormitory_id_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','dormitory_id')">{{form['dormitory_id']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','student_college') || $check_field('add','student_college') || $check_field('set','student_college')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生学院" prop="student_college">
					<el-input id="student_college" v-model="form['student_college']" placeholder="请输入学生学院"
							  v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','student_college')) || (!form['personal_information_id'] && $check_field('add','student_college'))" :disabled="disabledObj['student_college_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','student_college')">{{form['student_college']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','registered_residence_of_students') || $check_field('add','registered_residence_of_students') || $check_field('set','registered_residence_of_students')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生户籍" prop="registered_residence_of_students">
					<el-input id="registered_residence_of_students" v-model="form['registered_residence_of_students']" placeholder="请输入学生户籍"
							  v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','registered_residence_of_students')) || (!form['personal_information_id'] && $check_field('add','registered_residence_of_students'))" :disabled="disabledObj['registered_residence_of_students_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','registered_residence_of_students')">{{form['registered_residence_of_students']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','id_number') || $check_field('add','id_number') || $check_field('set','id_number')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="身份证号" prop="id_number">
					<el-input id="id_number" v-model="form['id_number']" placeholder="请输入身份证号"
							  v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','id_number')) || (!form['personal_information_id'] && $check_field('add','id_number'))" :disabled="disabledObj['id_number_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','id_number')">{{form['id_number']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','phone_number') || $check_field('add','phone_number') || $check_field('set','phone_number')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="手机号码" prop="phone_number">
					<el-input id="phone_number" v-model="form['phone_number']" placeholder="请输入手机号码" type="tel"
						v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','phone_number')) || (!form['personal_information_id'] && $check_field('add','phone_number'))">
					</el-input>
					<div v-else-if="$check_field('get','phone_number')">{{form['phone_number']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','student_email') || $check_field('add','student_email') || $check_field('set','student_email')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生邮箱" prop="student_email">
					<el-input id="student_email" v-model="form['student_email']" placeholder="请输入学生邮箱"
							  v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','student_email')) || (!form['personal_information_id'] && $check_field('add','student_email'))" :disabled="disabledObj['student_email_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','student_email')">{{form['student_email']}}</div>
				</el-form-item>
			</el-col>
			<el-col v-if="user_group === '管理员' || $check_field('get','student_major') || $check_field('add','student_major') || $check_field('set','student_major')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="学生专业" prop="student_major">
					<el-input id="student_major" v-model="form['student_major']" placeholder="请输入学生专业"
							  v-if="user_group === '管理员' || (form['personal_information_id'] && $check_field('set','student_major')) || (!form['personal_information_id'] && $check_field('add','student_major'))" :disabled="disabledObj['student_major_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','student_major')">{{form['student_major']}}</div>
				</el-form-item>
			</el-col>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item>
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				field: "personal_information_id",
				url_add: "~/api/personal_information/add?",
				url_set: "~/api/personal_information/set?",
				url_get_obj: "~/api/personal_information/get_obj?",
				url_upload: "~/api/personal_information/upload?",

				query: {
					"personal_information_id": 0,
				},

				form: {
					"student_name":'', // 学生姓名
					"student_number": 0, // 学生学号
					"student_gender":'', // 学生性别
					"student_age":'', // 学生年龄
					"class_name":'', // 班级名称
					"dormitory_building_id":'', // 宿舍楼ID
					"dormitory_id":'', // 宿舍ID
					"student_college":'', // 学生学院
					"registered_residence_of_students":'', // 学生户籍
					"id_number":'', // 身份证号
					"phone_number":'', // 手机号码
					"student_email":'', // 学生邮箱
					"student_major":'', // 学生专业
					"personal_information_id": 0, // ID

				},
				disabledObj:{
					"student_name_isDisabled": false,
					"student_number_isDisabled": false,
					"student_gender_isDisabled": false,
					"student_age_isDisabled": false,
					"class_name_isDisabled": false,
					"dormitory_building_id_isDisabled": false,
					"dormitory_id_isDisabled": false,
					"student_college_isDisabled": false,
					"registered_residence_of_students_isDisabled": false,
					"id_number_isDisabled": false,
					"phone_number_isDisabled": false,
					"student_email_isDisabled": false,
					"student_major_isDisabled": false,
				},
				// 用户列表
				list_user_student_number: [],
				// 用户组
				group_user_student_number: "",
				//学生性别选项列表
				list_student_gender: ['男','女'],
			}
		},
		methods: {
			/**
			 * 获取学生用户用户列表
			 */
			async get_list_user_student_number() {
                // if(this.user_group !== "管理员" && this.form["student_number"] === 0) {
                //     this.form["student_number"] = this.user.user_id;
                // }
                var json = await this.$get("~/api/user/get_list?user_group=学生用户");
                if(json.result && json.result.list){
                    this.list_user_student_number = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
			/**
			 * 获取学生用户用户组
			 */
			async get_group_user_student_number() {
				this.form["student_number"] = this.user.user_id;
				var json = await this.$get("~/api/user_group/get_obj?name=学生用户");
				if(json.result && json.result.obj){
					this.group_user_student_number = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_student_number(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_student_number.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
						for (let key in _this.form) {
							arrForm.push(key)
						}
						for (var i=0;i<arr.length;i++){
							for (var j=0;j<arrForm.length;j++){
								if (arr[i]===arrForm[j]){
									if (arr[i]!=="student_number") {
										_this.form[arrForm[j]] = res.result.obj[arr[i]]
										_this.disabledObj[arrForm[j] + '_isDisabled'] = true
										break;
									}else {
										_this.disabledObj[arrForm[j] + '_isDisabled'] = true
									}
								}
							}
						}
					}
				});
			},
			get_user_student_number(id){
				var obj = this.list_user_student_number.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},

			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
				/**
				* 请求列表前
				* @param {Object} param
				*/
				var user_group = this.user.user_group;
				if (user_group !== "管理员") {
					switch (user_group) {
						case "学生学号":
							if(param["student_number"] > 0){
								param["student_number"] = this.user.user_id;
							}
							break;
					}
				}
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
							}
						})
					})
				}
				$.db.del("form");
				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){
			},

			is_view(){
				var bl = this.user_group == "管理员";

				if(!bl){
					bl = this.$check_action('/personal_information/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/personal_information/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/personal_information/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/personal_information/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/personal_information/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
			this.get_list_user_student_number();
			this.get_group_user_student_number();
		},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
